package com.yourcompany.web.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yourcompany.api.dto.LoginLogQueryDTO;
import com.yourcompany.api.entity.LoginLog;
import com.yourcompany.api.service.LoginLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "登录日志管理")
@RestController
@RequestMapping("/api/login-log")
public class LoginLogController {
    
    @Autowired
    private LoginLogService loginLogService;
    
    @Operation(summary = "分页查询登录日志")
    @GetMapping("/page")
    public SaResult pageLoginLogs(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            LoginLogQueryDTO queryDTO) {
        Page<LoginLog> page = new Page<>(pageNum, pageSize);
        return SaResult.ok().setData(loginLogService.pageLoginLogs(page, queryDTO));
    }
    
    @Operation(summary = "获取最后一次登录记录")
    @GetMapping("/last/{userId}")
    public SaResult getLastLoginLog(@PathVariable Long userId) {
        return SaResult.ok().setData(loginLogService.getLastLoginLog(userId));
    }
    
    @Operation(summary = "清理指定天数之前的日志")
    @DeleteMapping("/clean/{days}")
    public SaResult cleanLoginLogs(@PathVariable Integer days) {
        loginLogService.cleanLoginLogs(days);
        return SaResult.ok();
    }
} 